package com.cake.liulanxiangzhu.controller;

import com.cake.liulanxiangzhu.pojo.entity.ContactInformation;
import com.cake.liulanxiangzhu.pojo.dto.ContactInformationDTO;
import com.cake.liulanxiangzhu.pojo.dto.ContactInformationUpdateDTO;
import com.cake.liulanxiangzhu.pojo.vo.ContactInformationVO;
import com.cake.liulanxiangzhu.service.ContactInformationService;
import com.cake.liulanxiangzhu.web.JsonPage;
import com.cake.liulanxiangzhu.web.JsonResult;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;
import org.springframework.validation.annotation.Validated;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * (ContactInformation)表控制层
 *
 * @author dinghao
 * @since 2023-02-21 21:45:42
 */
@RestController
@RequestMapping("/contactInformation")
@Validated
public class ContactInformationController {
    /**
     * 服务对象
     */
    @Autowired
    private ContactInformationService contactInformationService;

    /**
     * 分页查询
     *
     * @param contactInformation 筛选条件
     * @return 查询结果
     */
    @GetMapping("/queryContactInformationListPaging")
    public JsonResult<JsonPage<ContactInformationVO>> queryByPage(ContactInformation contactInformation, Integer page, Integer pageSize) {
        return JsonResult.ok(this.contactInformationService.queryByPage(contactInformation, page, pageSize));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryContactInformationById")
    public JsonResult<ContactInformationVO> queryById(Integer id) {
        return JsonResult.ok(this.contactInformationService.queryById(id));
    }

    /**
     * 查询微信默认地址
     *
     * @param request
     * @return 单条数据
     */
    @GetMapping("/queryContactInformationListPagingByUserId")
    public JsonResult<List<ContactInformationVO>> queryWxAddressListByUserId(HttpServletRequest request) {
        List<ContactInformationVO> contactInformationVOS = contactInformationService.queryWxAddressList(request);
        return JsonResult.ok(contactInformationVOS);
    }

    /**
     * 查询所有微信收货地址
     *
     * @param request
     * @return 单条数据
     */
    @GetMapping("/queryContactInformationByUserId")
    public JsonResult<ContactInformationVO> queryWxAddressByUserId(HttpServletRequest request) {
        ContactInformationVO contactInformationVO = contactInformationService.queryWxAddress(request);
        return JsonResult.ok(contactInformationVO);
    }


    /**
     * 新增数据
     *
     * @param contactInformationDto 实体
     * @return 新增结果
     */
    @PostMapping("/insertContactInformation")
    public JsonResult<Void> add(HttpServletRequest request,@Valid  @RequestBody ContactInformationDTO contactInformationDto) {
        System.out.println("request = " + request + ", contactInformationDto = " + contactInformationDto);
        this.contactInformationService.insert(request,contactInformationDto);
        return JsonResult.ok();
    }

    /**
     * 编辑数据
     *
     * @param contactInformationUpdateDto 实体
     * @return 编辑结果
     */
    @PostMapping("/updateContactInformation")
    public JsonResult<Void> edit(HttpServletRequest request, @RequestBody ContactInformationUpdateDTO contactInformationUpdateDto) {
        this.contactInformationService.update(request,contactInformationUpdateDto);
        return JsonResult.ok();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/deleteContactInformation")
    public JsonResult<Void> deleteById(Integer id) {
       this.contactInformationService.deleteById(id);
       return JsonResult.ok();
    }

}

